//
//  ReSetViewController.m
//  WGFGlobalHouses
//
//  Created by zhouhui on 15/4/19.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "ReSetViewController.h"
#import "UIView+circular.h"
#import "SettingDataManage.h"
#import "MBProgressHUD+MJ.h"
#import "Connect.h"
#import "StringTool.h"
#import "CheckNetWorkTool.h"
#import "UserModal.h"
#import "LoginViewController.h"
#import "UIView+circular.h"

#import "config.h"
@interface ReSetViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *mainView;

@property (weak, nonatomic) IBOutlet UIView *view1;
@property (weak, nonatomic) IBOutlet UIView *view2;
@property (weak, nonatomic) IBOutlet UIView *view3;

@property (weak, nonatomic) IBOutlet UITextField *olderPwdTextField;
@property (weak, nonatomic) IBOutlet UITextField *PwdTextField;
@property (weak, nonatomic) IBOutlet UITextField *repeatPwdTextFiled;
@property (strong, nonatomic) SettingDataManage *dataManage;

@property (weak, nonatomic) IBOutlet UIView *backView;


@property (weak, nonatomic) IBOutlet UIButton *saveBtn;

- (IBAction)clickBtn:(UIButton *)sender;


@end

@implementation ReSetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    
}

-(SettingDataManage *)dataManage
{
    if (!_dataManage)
    {
        _dataManage = [[SettingDataManage alloc]init];
    }
    return _dataManage;
}
-(void)initView
{
    
    [self.saveBtn viewToCircular];
    [self.titleView setMianTitle:@"密码设置"];
    self.titleView.doneBtn.hidden = YES;
    
    
    [self.view1 viewToCircular];
    [self.view2 viewToCircular];
    [self.view3 viewToCircular];
    
}

#pragma mark 判断输入的密码格式正确
-(BOOL)checkPwd
{
    if ([StringTool isBlankString:self.olderPwdTextField.text]) {
        [MBProgressHUD showError:@"请输入当前密码"];
        return NO;
    }else if ([StringTool isBlankString:self.PwdTextField.text]){
        [MBProgressHUD showError:@"请输入新密码"];
        return NO;
    }else if ([StringTool isBlankString:self.olderPwdTextField.text]){
        [MBProgressHUD showError:@"请再次输入新密码"];
        return NO;
    }else if (![self.PwdTextField.text isEqualToString:self.repeatPwdTextFiled.text])
    {
        [MBProgressHUD showError:@"两次新密码输入不一致"];
        return NO;
    }
    return YES;
}
- (IBAction)clickBtn:(UIButton *)sender

{
    if ([self checkPwd]) {
        [self modifyPwd];
    }
}

#pragma mark 提交数据
-(void)modifyPwd
{
    if ([self checkPwd]) {
        [MBProgressHUD showMessage:@"请稍等" toView:self.view];
        if ([CheckNetWorkTool isExistenceNetwork])
        {
            __weak typeof(self)bself = self;
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                
                NSString *customerId =  [NSString stringWithFormat:@"%zd",[UserModal shareUserModel].userId];
                NSString *oldPwd = bself.olderPwdTextField.text;
                NSString *newPwd = bself.PwdTextField.text;
                
                ResulteMod *mod =  [bself.dataManage modifypwdWithCustomerId:customerId WithOldPwd:oldPwd WithNewPwd:newPwd];
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    [MBProgressHUD hideHUDForView:bself.view animated:YES];
                    
                    if (mod.resltState == ResultSuccess)
                    {
                        [bself.navigationController pushViewController:[LoginViewController new
                                                                       ] animated:YES];
                        [MBProgressHUD showSuccess:@"密码修改成功，请重新登录"];
                    }else if (mod.resltState == ResultFail)
                    {
                        [bself.navigationController popViewControllerAnimated:YES];
                        [MBProgressHUD showError:mod.resultTip];
                    }else
                    {
                        [bself.navigationController popViewControllerAnimated:YES];
                        [MBProgressHUD showError:@"网络错误"];
                    }
                    
                });
            });
            
        }else
        {
            [MBProgressHUD hideHUD];
            [MBProgressHUD showError:@"网络没连接!"];
            
        }
  
    }
}



- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
    
    CGRect dd =  [textField.superview convertRect:textField.frame toView:self.backView];
    
    int tvbotY = dd.size.height+dd.origin.y;
    int mid  = PMH-[UserModal shareUserModel].keyboardHeight-tvbotY -64;
    
    if (mid<0) {
        
        const float movementDuration = 0.3f;
        [UIView beginAnimations: nil context: nil];
        [UIView setAnimationBeginsFromCurrentState: YES];
        [UIView setAnimationDuration: movementDuration];
        
        CGRect buf = self.backView.frame;
        buf.origin.y = up ? mid : 0;
        [self.backView setFrame:buf];
        
        [UIView commitAnimations];
    }else if(up==false){
        
        [self viewRest];
    }
}



-(void)viewRest{
    
    
    const float movementDuration = 0.3f;
    [UIView beginAnimations: nil context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];
    
    CGRect buf = self.backView.frame;
    buf.origin.y =  0;
    [self.backView setFrame:buf];
    
    [UIView commitAnimations];
}

#pragma et delegare
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self animateTextField: textField up: YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    [self animateTextField:textField up:false];
    
    [textField resignFirstResponder];
    
    [self viewRest];
    return  YES;
}
@end
